<?php
class AreasController extends AppController {

	var $name = 'Areas';

	function index() {
		$this->Area->recursive = 0;
		$this->set('areas', $this->paginate());
	}

	function admin_index() {
		$this->Area->recursive = 0;
		$this->set('areas', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid area', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('area', $this->Area->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Area->create();
			if ($this->Area->save($this->data)) {
				$this->Session->setFlash(__('The area has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The area could not be saved. Please, try again.', true));
			}
		}
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid area', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Area->save($this->data)) {
				$this->Session->setFlash(__('The area has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The area could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Area->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for area', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Area->delete($id)) {
			$this->Session->setFlash(__('Area deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Area was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	// --------------------------------------------------------------------------------
	function admin_areaswithtables(){
		$this->Area->recursive = 1;
		$areaswithtables = $this->Area->find('all');
		// Duyệt từng table và xác định coi table đã có gọi món hay chưa (order)
		$this->loadModel('Order');
		$i=0;
		foreach($areaswithtables as $area){
			$tableInArea = $area['Table'];
			$j=0;
			foreach($tableInArea as $table){
				if($this->Order->isOdered($table['id'])){
					$areaswithtables[$i]['Table'][$j]['ordered'] = 1;
				}
				else{
					$areaswithtables[$i]['Table'][$j]['ordered'] = 0;
				}
				if($this->Order->isOrderingPrepared($table['id'])){
					$areaswithtables[$i]['Table'][$j]['prepared'] = 1;
				}
				else{
					$areaswithtables[$i]['Table'][$j]['prepared'] = 0;
				}
				$j++;
			}
			$i++;
		}
		$this->set('areaswithtables',$areaswithtables);
		//print_r($areaswithtables);
		$this->layout=false;
		//$this->autoRender=false;
	}
	
	function admin_tablesinareas($areasid = null){
		$this->Area->recursive = 1;
		if(isset($areasid) && is_numeric($areasid)){
			$tables = $this->Area->read(null, $areasid);
			$this->set('tables',$tables);
		}
		$this->layout=false;
	}
}